<template>
  <div class="app-container">
    <div class="table-container">
      <div class="left">
        <to-link :val="'YdTHyHh0i'" @to="handleOpen">
          <el-button size="small" type="primary">添加</el-button>
        </to-link>
      </div>
      <div class="right">
        <el-radio-group v-model="search_type" size="small" @change="search_type_value">
          <el-radio-button border :key="`0`" :label="`0`">全部</el-radio-button>
          <el-radio-button border v-for="item in ad_type" :key="item.value" :label="item.value">{{item.label}}</el-radio-button>
        </el-radio-group>
      </div>
    </div>
    <div class="_list">
      <el-table ref="tableSelection" highlight-current-row @selection-change="handleSelectionChange" :data="dataList" style="width: 100%">
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column label="地址名称">
          <template slot-scope="scope" width="100">
            {{scope.row.name}}
          </template>
        </el-table-column>
        <el-table-column label="门店类型">
          <template slot-scope="scope">
            {{scope.row.stores_type.join(',')}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="handleUpdata(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      </div>
      <div class="pages">
        <div class="left">
          <to-link :val="'PmEc3qVnn'" @to="handleAll">
            <el-tooltip class='item' effect='dark' content='批量删除' placement='top'>
              <el-button size="small" type="danger">批量删除</el-button>
            </el-tooltip>
          </to-link>
        </div>
        <div class="right" v-if="dataList.length > 1">
          <el-pagination @current-change="handleCurrentChange" background :page-size="4" :current-page.sync="page" layout="prev, pager, next" :total="total">
          </el-pagination>
        </div>
      </div>
    <el-dialog title="地址添加" :visible.sync="dialogVisible" width="380px" top="20vh">
      <el-form ref="dialogForm" :model="form" :rules="colorFormRules" label-width="80px">
        <el-form-item label="门店类型" prop="stores_type">
          <el-checkbox-group v-model="stores_type" size="small">
            <el-checkbox v-for="item,key in ad_type" :key="key" :label="item.label" border></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="地址名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleCancel">取消</el-button>
        <el-button size="small" type="primary" @click="handleSubmit('dialogForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
  </div>
</template>
<script>
import Service from '@/api/service'
import mixinsTable from '@/utils/mixins.table'
export default {
  name: 'addressList',
  mixins: [mixinsTable],
  data() {
    return {
      pathsState: 'add',
      listLoading: false,
      addressAddVisible: false,
      productAddVisible: false,
      multipleSelection: [],
      page: 1,
      total: 0,
      stores_type: [],
      dataList: [],
      _selection: [],
      ad_type: [{
        value: '1',
        label: '直营店'
      }, {
        value: '2',
        label: '加盟店'
      }],
      search_type: '0',
      form: {
        name: ''
      },
      colorFormRules: {
        name: [
          { required: true, message: '名称不能为空', trigger: 'blur' }
        ],
        stores_type: [
          { required: true, message: '门店类型不能为空', trigger: 'change' }
        ]
      }
    }
  },
  computed: {
    moduleService() {
      return new Service('/pages/address')
    }
  },
  created() {
    this.multipleSelection = this.selection
    this.getList()
  },
  methods: {
    search_type_value(val) {
      if (val !== '0') {
        this.$set(this.params, 'stores_type', val)
      } else {
        delete this.params.stores_type
      }
      this.getList()
    },
    initForm(row) {
      if (row.stores_type) {
        this.$set(this, 'stores_type', row.stores_type)
      }
      this.setFormVal(row)
    },
    FormClear() {
      this.$set(this, 'stores_type', [])
      this.setFormVal(this.form)
    },
    submitData() {
      this.$set(this.form, 'stores_type', this.stores_type)
    },
    setFormVal(row) {
      const formArr = Object.keys(row)
      formArr.map(item => {
        this.$set(this.form, item, this.dialogState === 'update' ? row[item] : '')
      })
    },
    handleCurrentChange(val) {
      this.getList(val)
    }
  }
}

</script>
<style lang="scss" scoped>
.address-body {
  margin: -30px 0;
}
</style>
